﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Animation;
using UmlControlLibrary;
using System.Security;
using System.Windows.Interop;
using System.Runtime.InteropServices;

namespace UmlControlLibrary
{
    /// <summary>
    /// Interaction logic for UmlInterfaceControl.xaml
    /// </summary>
    public partial class UmlNoteControl : UmlDesignerElement
    {
        private static ResourceDictionary ControlResources = Application.LoadComponent(new Uri("/UmlControlLibrary;component/DiagramElementsControls/ResourceDictionaries/UmlNoteControlResources.xaml", UriKind.Relative)) as ResourceDictionary;

        public static void RegisterControl()
        {
            SpecificMenu menu = SpecificMenu.GetTypeMenu(typeof(UmlDiagramControl));
            menu.AddItem(new SpecificMenuItem("Add note", "Add", AddNote));
        }

        public UmlNoteControl()
            : base()
        {
            InitializeComponent();

            this.Resources.MergedDictionaries.Add(ControlResources);
            this.Template = (ControlTemplate)this.Resources["UmlNoteTemplate"];
            this.ApplyTemplate();

            this.AddDragZone(this.Template.FindName("rect1", this) as UIElement);
            this.AddDragZone(this.Template.FindName("rect2", this) as UIElement);
            this.AddDragZone(this.Template.FindName("content", this) as UIElement);
        }

        public override void AddSections()
        {
            base.AddSections();
        }
      
        public static void AddNote(object sender, RoutedEventArgs e)
        {
            ContextMenuEventArgs args = ((sender as Control).Tag as UmlContextMenu).MenuEventArgs;
            UmlDiagramControl diagram = (sender as MenuItem).DataContext as UmlDiagramControl;
            UmlNote _note = new UmlNote("New_Note");
            diagram.InsertionPos = new Point(args.CursorLeft, args.CursorTop);           
            UmlModel.CurrentDiagram.AddElement(_note);
        }
        
        protected override Control GetControlActionsDecorator()
        {
            Control c = base.GetControlActionsDecorator();
            if (c == null) 
            {
                ControlTemplate t = GetActionsDecoratorTemplate();
                RegisterActionDecorator(t, "btnTop", "Note.png", new MouseButtonEventHandler(button1_Drag), "Link note to diagram element");

                c = GetExistingActionsDecorator();
                c.Template = t;
                c.ApplyTemplate();
            }

            return c;           
        }
      
        private void button1_Drag(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                LineDragThumb t1 = new LineDragThumb(this,e);
                t1.OnEndDrag += new RoutedEventHandler(t1_OnEndDrag);
            }
        }

        void t1_OnEndDrag(object sender, RoutedEventArgs e)
        {
            SelectableItemControl target = (e.Source as LineDragThumb).SelectedTarget;
            if (target != null)
                (this.DataContext as UmlNote).LinkNote(target.DataContext as UmlElement);
        }
    }

}
